local description = [[
# 混战模式

所有玩家都需要为自己而战！既要谋略，亦要实力，不再受后置位坐牢之苦，好好发挥出你的真实水准吧。

规则如下：

- 胜利条件为击杀所有其他角色，成为唯一的生存者。
- 击杀后摸2张牌。
- 采用常规回合流程，按顺序轮流行动。
- 游戏开始时随机决定先后手顺序，首个行动的玩家为先手。

]]

local test_1_getLogic = function()
  local test_1_logic = GameLogic:subclass("test_1_logic")

  function test_1_logic:assignRoles()
    local room = self.room
    local n = #room.players

    -- 所有玩家身份设为隐藏（不影响先后手）
    for i = 1, n do
      local p = room.players[i]
      p.role = "hidden"
      room:setPlayerProperty(p, "role_shown", true)
      room:broadcastProperty(p, "role")
    end

    self.start_role = "hidden"
    -- 仅为兼容座位调整逻辑，不影响实际先后手
    room.players[1].role = "lord"
  end

  function test_1_logic:chooseGenerals()
    local room = self.room
    local generalNum = room.settings.generalNum
    local n = room.settings.enableDeputy and 2 or 1  -- 是否启用副将
    local lord = room:getLord()
    room:setCurrent(lord)
    lord.role = self.start_role

    local players = room.players
    local generals = room:getNGenerals(#players * generalNum)
    local req = Request:new(players, "AskForGeneral")
    table.shuffle(generals)  -- 打乱武将池

    -- 分配武将选择池
    for i, p in ipairs(players) do
      local arg = table.slice(generals, (i - 1) * generalNum + 1, i * generalNum)
      req:setData(p, { arg, n })
      req:setDefaultReply(p, table.random(arg, n))
    end

    req:ask()  -- 发送武将选择请求

    -- 处理选择结果
    local selected = {}
    for _, p in ipairs(players) do
      local gs = req:getResult(p)
      local general = gs[1]
      local deputy = gs[2]
      room:setPlayerGeneral(p, general, true, true)
      if deputy then
        room:setDeputyGeneral(p, deputy)
      end
      table.insert(selected, general)
      if deputy then table.insert(selected, deputy) end
    end

    -- 归还未被选择的武将
    local remaining = table.filter(generals, function(g)
      return not table.contains(selected, g)
    end)
    room:returnToGeneralPile(remaining)

    -- 选择势力
    room:askToChooseKingdom(players)
  end

  -- 新增：决定先后手顺序（在游戏开始时调用）
  function test_1_logic:determineTurnOrder()
    local room = self.room
    local players = room.alive_players  -- 获取所有存活玩家
    local n = #players

    -- 随机打乱玩家顺序，首个玩家为先手
    table.shuffle(players)
    
    -- 记录回合顺序（后续按此顺序轮流行动）
    room.turn_order = players
    
    -- 通知所有玩家先后手顺序
    local orderText = "先后手顺序："
    for i, p in ipairs(players) do
      orderText = orderText .. string.format("%d. %s  ", i, p:getScreenName())
    end
    room:broadcastNotify("ShowToast", orderText)
    
    -- 设置首个行动玩家
    room:setCurrent(players[1])
    room:broadcastProperty(players[1], "isCurrent")
  end

  -- 重写开始游戏逻辑，加入先后手判定
  function test_1_logic:startGame()
    self:assignRoles()
    self:chooseGenerals()
    self:determineTurnOrder()  -- 新增：决定先后手
    self.room:startGame()  -- 启动游戏
  end

  return test_1_logic
end

local test_1_mode = fk.CreateGameMode{
  name = "test_1_mode",
  minPlayer = 2,
  maxPlayer = 8,
  logic = test_1_getLogic,

  -- 投降判定：只剩两人时可投降
  surrender_func = function(self, playedTime)
    local alive = table.filter(Fk:currentRoom().players, function(p)
      return p.rest > 0 or not p.dead
    end)
    return { { text = "混战模式：仅剩两人时可投降", passed = #alive == 2 } }
  end,

  -- 击杀奖励：摸2张牌
  reward_punish = function (self, victim, killer)
    if killer and not killer.dead then
      killer:drawCards(2, "击杀奖励")
    end
  end,

  -- 胜利判定：唯一存活者获胜
  winner_getter = function(self, victim)
    if not victim.surrendered and victim.rest > 0 then
      return ""
    end

    local room = victim.room
    local alive = table.filter(room.players, function(p)
      return not p.surrendered and not (p.dead and p.rest == 0)
    end)

    if #alive == 1 then
      local winner = alive[1]
      winner.role = "renegade"
      if room.class.name == 'Room' then
        room:broadcastProperty(winner, 'role')
      end
      return "renegade"
    end

    return ""
  end,
}

-- 加载翻译文本
Fk:loadTranslationTable{
  ["test_1_mode"] = "测试模式_1",
  [":test_1_mode"] = description,
}

return test_1_mode